package com.example.dbservice.controller;

import com.example.dbservice.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.hcl.common.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Author: cailong
 * @Date: 2021/6/4 19:18
 * @Description:
 */
@RestController
@Slf4j
public class UserController {

    @Resource
    private UserRepository userRepository;

    /**
     * 根据uid,查询对应的User
     * @param uid
     * @return
     */
    @RequestMapping("/send/{uid}")
    public User send(@PathVariable("uid") String uid){
        log.info("finding uid: "+uid);
        Optional<User> opUser = userRepository.findById(uid);
        if (!opUser.isPresent()) //没有找到对应uid,返回null
            return null;
        log.info(String.valueOf(opUser.get()));
        return opUser.get();
    }

    /**
     * 保存新用户
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/save")
    public User save(String username,String password){
        User user = new User();
        user.setUserName(username);
        user.setPassWord(password);
        user = userRepository.save(user);
        log.info("save user:"+user);

        return user;
    }
}
